*********************************************************************************
* * Table B1: Attrition Analysis
*********************************************************************************
preserve

* Table 1: Balance test between matched and unmatched students 
foreach x in $balancevars {
	gen treatxtimesx`x' = treatment*`x'
	local varl: variable label `x'
	la var treatxtimesx`x' "`varl'"
}

gen matched = have_siel == 1 & have_aserel == 1

*BASELINE AND ENDLINE
est clear 
*eststo: reg matched treatment $balancevars treatxtimesx*, vce(robust)

foreach var of varlist $balancevars {
	qui eststo: reg matched treatment `var' treatxtimesx`var', vce(robust)
	mat B = nullmat(B) \ _b[treatxtimesx`var']
	mat SE = nullmat(SE) \ _se[treatxtimesx`var']
	mat T = nullmat(T) \ 2*ttail(e(df_r),abs(_b[treatxtimesx`var']/_se[treatxtimesx`var']))
}
mat bhat = (B,SE,T)
mat colnames bhat = "Coefficient" "SE" "p-value"
mat rownames bhat = "Female" "Age" "Hindu" "SC/ST" "OBC" "Illiterate" "High-School" "Income > Rs. 10k" "ASER Reading" "ASER Maths" "Parental Support" "Life Skills" "Attitudes"

estout matrix(bhat) using "$tables/Attrition.csv", delimiter(",") tit("TABLE D1: ATTRITION") replace 



****************************
* LEE BOUNDS
****************************
foreach var of varlist el_read el_math {
	qui eststo: bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui leebounds `var' treatment, level(`c')
	mat L = nullmat(L) \ (_b[lower] - 1.645*_se[lower], _b[lower] + 1.645*_se[lower])
	mat L = L \ (_b[lower] - 1.96*_se[lower], _b[lower] + 1.96*_se[lower])
	mat L = L \ (_b[lower] - 2.576*_se[lower], _b[lower] + 2.576*_se[lower])

	mat U = nullmat(U) \ (_b[upper] - 1.645*_se[upper], _b[upper] + 1.645*_se[upper])
	mat U = U \ (_b[upper] - 1.96*_se[upper], _b[upper] + 1.96*_se[upper])
	mat U = U \ (_b[upper] - 2.576*_se[upper], _b[upper] + 2.576*_se[upper])
	noi di "`var': Done"
	mat bhat = (L,U)
	mat colnames bhat = "LB (Low)" "LB (Upper)" "UB (Low)" "UB (Upper)"
	mat rownames bhat = "90th percentile" "95th percentile" "99th percentile"
	estout matrix(bhat) using "$tables/Attrition.csv", delimiter(",") tit("TABLE D1: ATTRITION") append
	mat drop L U
}
restore

noi di "Done with Attrition Tables"


